<?php

class Web_Mail extends Web_BasePage
{

    public function mainContent()
    {
        if (!Ey_Login::isLogged()) {
            Ey::redirect(WEB_ROOT . '/login');
        }

        $car_id = Ey::getPrm(1);

        if ($car_id) {

            $obj3 = new Web_Db_Carrito();
            $db3 = $obj3->getAdapter();
            $caritoDato = $db3->fetchRow($obj3->select()
                            ->where('car_id=?', $car_id)
                            ->where('car_usu_id=?', $_SESSION['usr']['cli_id']));

            if ($caritoDato) {
                $obj = new Web_Db_CarritoDetalle();
                $db = $obj->getAdapter();
                $carrito = $db->fetchAll($obj->select()
                                ->where('det_car_id=?', $car_id));

                foreach ($carrito as $value) {
                    $total+=$value->det_pro_precio;
                }

                $obj2 = new Web_Db_CarritoDireccion();
                $db2 = $obj2->getAdapter();
                $direccion = $db2->fetchRow($obj2->select()
                                ->where('dir_car_id=?', $car_id));

                $obj4 = new Web_Db_Clientes();
                $db4 = $obj4->getAdapter();
                $cliente = $db4->fetchRow($obj4->select()
                                ->where('cli_id=?', $_SESSION['usr']['cli_id']));

                global $db;
                $sql = $db->select()->from('ma_moneda', array('mon_valor'))->where('mon_key="dolar"');
                $moneda = $db->fetchRow($sql);
                $moneda->mon_valor = round(1 / $moneda->mon_valor, 2);
                
                $totalSoles = $caritoDato->car_total * $moneda->mon_valor;
            }
        }

        $smarty = new Smarty_Engine();
        $smarty->assign('car_id', $car_id);
        $smarty->assign('total', $total);
        $smarty->assign('carrito', $carrito);
        $smarty->assign('direccion', $direccion);
        $smarty->assign('caritoDato', $caritoDato);
        $smarty->assign('cliente', $cliente); 
        $smarty->assign('totalSoles', number_format($totalSoles, 2, '.', ','));
        $smarty->assign('moneda', $moneda->mon_valor);

        return $smarty->fetch(APP_ROOT . DS . 'tpl' . DS . 'mail-demo.tpl');
    }

}